package com.zhongke.cotrun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhongke.cotrun.mapper.IntegralMapper;
import com.zhongke.cotrun.mapper.UserInfoMapper;
import com.zhongke.cotrun.model.pojo.Integral;
import com.zhongke.cotrun.model.pojo.IntegralRecord;
import com.zhongke.cotrun.model.pojo.IntegralVo;
import com.zhongke.cotrun.service.IntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IntegralServiceImpl implements IntegralService {
    @Autowired
    IntegralMapper integralMapper;
    @Autowired
    UserInfoMapper userInfoMapper;

    /**
     * 新增积分规则
     *
     * @param integral
     * @return
     */
    @Override
    public boolean insertIntegral(Integral integral) {


        return integralMapper.insertIntegral(integral);
    }

    /**
     * 根据条件搜所具体的
     *
     * @param integralId
     * @return
     */
    @Override
    public Integral selectIntegralByid(Integer integralId) {
        return integralMapper.selectIntegralById(integralId);
    }

    /**
     * 根据条件搜索分页展示类型
     *
     * @param integral
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo selectIntegralByconditon(Integral integral, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);

        List<IntegralVo> l = integralMapper.selectAllIntegral(integral);
        for (IntegralVo i : l) {
            i.setCreateName(userInfoMapper.selectSysUserNameById(i.getCreateId()));

        }

        PageInfo p = new PageInfo(l);

        return p;
    }

    /**
     * 根据条件搜索积分类型列表
     *
     * @param integral
     * @return
     */
    @Override
    public List<IntegralVo> selectAllIntegral(Integral integral) {

        List<IntegralVo> l = integralMapper.selectAllIntegral(integral);
        for (IntegralVo i : l) {
            i.setCreateName(userInfoMapper.selectSysUserNameById(i.getCreateId()));

        }

        return l;
    }

    /**
     * 修改积分类型
     *
     * @param integral
     * @return
     */
    @Override
    public boolean updateIntegral(Integral integral) {


        return integralMapper.updateIntegral(integral);
    }

    /**
     * 修改积分类型状态修改
     *
     * @param state
     * @param integerId
     * @return
     */
    @Override
    public Boolean updateIntegralRecord(Integer state, Integer integerId) {
        return integralMapper.updateInteStatue(state, integerId);
    }

    /**
     * 积分规则后台搜索
     *
     * @param integral
     * @return
     */
    @Override
    public PageInfo selectIntegralAndRecord(Integral integral, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);

        List<IntegralVo> l = integralMapper.selectIntegralAndRecord(integral);

        if (!l.isEmpty()) {
            for (IntegralVo i : l) {
            IntegralRecord integralRecord= i.getIntegralRecord();
            if(integralRecord!=null){
                i.setCreateName(userInfoMapper.selectSysUserNameById(integralRecord.getLinkUserId()));

            }

            }


        }


        return new PageInfo(l);
    }
}
